package com.application.sistercityapplication.main;

import java.util.ArrayList;
import java.util.List;

import com.application.sistercityapplication.mappage.FullScreenMapViewActivity;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("ValidFragment")
public class CountryMenuFragment extends ListFragment {
	private List<Integer> _menuList;
	private ViewHolderForFirst viewHolderForFirst;
	private ViewHolderForNext viewHolderForNext;
	private String _countryTitle;
	private int country_id;
	private SlidingMenu sm;
	public CountryMenuFragment(String countryTitle,int COUNTRY_ID,SlidingMenu sm){
		this._countryTitle = countryTitle;
		this.country_id = COUNTRY_ID;
		this.sm = sm;
	}
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		_menuList = new ArrayList<Integer>();
		for (int i = 0; i <= 8; i++) {
			_menuList.add(i);
		}
		MenuListAdapter mla = new MenuListAdapter();
		setListAdapter(mla);
	}

	@Override
	public void onListItemClick(ListView lv, View v, int position, long id) {
		Fragment newContent = null;
		switch (position) {
		case 0:
			//Home
			newContent = new MainPageFragment(country_id,_countryTitle,sm);
			break;
		case 1:
			//Link to map page
			Intent i = new Intent(getActivity(),FullScreenMapViewActivity.class);
			i.putExtra("countryId", country_id);
			i.putExtra("countryName", _countryTitle);
			startActivity(i);
			break;
		case 2:
			//Link to information page
			newContent = new GeneralInfoFragment(country_id,sm,_countryTitle);
			break;
		case 3:
			//Link to document page
//			newContent = new DocumentPageFragment(country_id,_countryTitle,sm);
			sm.toggle();
			Intent intent = new Intent(getActivity(),MainNewsDockViewActivity.class);
			intent.putExtra("countryId", country_id);
			intent.putExtra("countryName", _countryTitle);
			intent.putExtra("menu", 0);
			startActivity(intent);
			
			break;
		case 4:
			//Link to Knowledge mangement
//			newContent = new MainPageFragment(country_id,_countryTitle,sm);
			sm.toggle();
			Intent intent2 = new Intent(getActivity(),MainNewsDockViewActivity.class);
			intent2.putExtra("countryId", country_id);
			intent2.putExtra("countryName", _countryTitle);
			intent2.putExtra("menu", 1);
			startActivity(intent2);
			break;
		case 5:
//			newContent = new MainPageFragment(country_id,_countryTitle,sm);
			sm.toggle();
			Intent intent3 = new Intent(getActivity(),MainNewsDockViewActivity.class);
			intent3.putExtra("countryId", country_id);
			intent3.putExtra("countryName", _countryTitle);
			intent3.putExtra("menu", 2);
			startActivity(intent3);
			break;
		case 6:
			//Link to News Page
			newContent = new NoticeAndNewsFragment(country_id,sm,_countryTitle);
			break;
		case 7:
			//Link to Gallery Page
			newContent = new MainCatImageActivity(country_id,_countryTitle,sm);
			break;
		case 8:
			//Link to Contact point page
			newContent = new ContactPointFragment(country_id,_countryTitle,sm);
			break;
		}
		if (newContent != null)
			switchFragment(newContent);
	}

	// the meat of switching the above fragment
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof NewDashBoardPageActivity) {
			NewDashBoardPageActivity fca = (NewDashBoardPageActivity) getActivity();
			fca.switchContent(fragment);
		}
	}

	public class MenuListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return _menuList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return _menuList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			if (position == 0) {
				if (convertView == null) {
					viewHolderForFirst = new ViewHolderForFirst();
					LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					convertView = inflater.inflate(R.layout.menu_list_first,null);
					viewHolderForFirst.countryTitle = (TextView) convertView.findViewById(R.id.textView2);
					viewHolderForFirst.settingBtn = (ImageView) convertView.findViewById(R.id.imageView2);
					convertView.setTag(viewHolderForFirst);
				} else {
					viewHolderForFirst = (ViewHolderForFirst) convertView.getTag();
				}
					viewHolderForFirst.countryTitle.setText(getActivity().getString(R.string.country_text)+_countryTitle);
					viewHolderForFirst.settingBtn.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							Intent i = new Intent(getActivity(),MainSettingPageViewActivity.class);
							startActivity(i);
						}
					});
			} else {
				if (convertView == null) {
					viewHolderForNext = new ViewHolderForNext();
					LayoutInflater inflater = (LayoutInflater) getActivity()
							.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					convertView = inflater.inflate(R.layout.menu_list_next,
							null);
					viewHolderForNext.menoLogo = (ImageView) convertView
							.findViewById(R.id.imageView1);
					viewHolderForNext.menuTitle = (TextView) convertView
							.findViewById(R.id.textView1);
					convertView.setTag(viewHolderForNext);
				} else {
					viewHolderForNext = (ViewHolderForNext) convertView
							.getTag();
				}
				switch (position) {
				case 1:
					viewHolderForNext.menoLogo.setImageDrawable(getResources().getDrawable(R.drawable.menu_map_icon));
					viewHolderForNext.menuTitle.setText(getResources().getString(R.string.menu_list_map));
					break;
				case 2:
					viewHolderForNext.menoLogo.setImageDrawable(getResources().getDrawable(R.drawable.menu_docuement_icon));
					viewHolderForNext.menuTitle.setText(getResources().getString(R.string.informationText));
					break;
				case 3:
					viewHolderForNext.menoLogo.setImageDrawable(getResources().getDrawable(R.drawable.menu_docuement2_icon));
					viewHolderForNext.menuTitle.setText(getResources().getString(R.string.menu_list_document));
					break;
				case 4:
					viewHolderForNext.menoLogo.setImageDrawable(getResources().getDrawable(R.drawable.menu_km_icon));
					viewHolderForNext.menuTitle.setText(getResources().getString(R.string.menu_list_mk));
					break;
				case 5:
					viewHolderForNext.menoLogo.setImageDrawable(getResources().getDrawable(R.drawable.menu_memo_icon));
					viewHolderForNext.menuTitle.setText(getResources().getString(R.string.menu_list_memo));
					break;
				case 6:
					viewHolderForNext.menoLogo.setImageDrawable(getResources().getDrawable(R.drawable.menu_news_icon));
					viewHolderForNext.menuTitle.setText(getResources().getString(R.string.menu_list_news));
					break;
				case 7:
					viewHolderForNext.menoLogo.setImageDrawable(getResources().getDrawable(R.drawable.menu_gallery_icon));
					viewHolderForNext.menuTitle.setText(getResources().getString(R.string.menu_list_gallery));
					break;
				case 8:
					viewHolderForNext.menoLogo.setImageDrawable(getResources().getDrawable(R.drawable.menu_contact_icon));
					viewHolderForNext.menuTitle.setText(getResources().getString(R.string.menu_list_contact));
					break;

				default:
					break;
				}
			}

			return convertView;
		}

	}

	public class ViewHolderForFirst {
		public TextView countryTitle ;
		public ImageView settingBtn;
	}

	public class ViewHolderForNext {
		public ImageView menoLogo;
		public TextView menuTitle;
	}

}
